
import {
	userIsLogin,
	getWeChartSmallProgramLoginInfo,
	dcH5WechatLogin, 
	searchUserInfoById,
	getBusinessInfo
 } from '@/tools/request/api.js'
 import JSONBig from 'json-bigint';
 
 

export default {
	namespaced: true,
	state: () => ({
		businessList: [],
		businessId: uni.getStorageSync('__businessId') || '',
		storeInfo: uni.getStorageSync('__ddminiStoreInfo') || {},
		isDone: false, // 是否登录完成，以及获取到了首页所需要的所有参数，避免因请求时机问题造成首页数据获取失败。
		doneCall: '',
		callList: []
	}),
	mutations: {
		// 设置店铺信息
		SET_STORE_INFO (state, {busiList, index}) {
			if (!busiList.length) return uni.$showMsg('暂无店铺'); 
			let currentStore = busiList[index]; // 默认是店铺列表的第一个
			let storeInfo = JSON.parse(currentStore.jsonData);
			state.storeInfo = storeInfo;
			uni.setStorageSync('__ddminiStoreInfo', storeInfo);
			state.businessList = busiList;
			let businessId = JSONBig.stringify(currentStore.businessId);
			state.businessId = businessId;
			uni.setStorageSync('__businessId', businessId);
			state.isDone = true;
			this.commit('business_store/IS_BUSINESS_DONE');
		},
		// 是否初始化完成
		IS_BUSINESS_DONE (state, call) {
			// if (call) {
			// 	state.doneCall = call;
			// }
			// if (!state.isDone) return;
			// if (!state.doneCall) return;
			// state.doneCall(true);
			
			
			if (call) {
				state.callList.push(call);
			}
			if (!state.isDone) return;
			if (!state.callList.length) return;
			state.callList.forEach((item) => {
				item();
			})
			state.callList = [];
		}
	},
	actions: {
		// 获取店铺信息
		async getBusinessInfo ({commit, rootState}, state) {
			let params = {
				latitude: 0,
				longitude: 0
			}
			let res = await getBusinessInfo(params);
			if (res.code != 200) return uni.$showMsg();
			if (!res.body.length) return;
			commit('SET_STORE_INFO', {busiList: res.body, index: 0}); // 默认第一个店铺
			rootState.loading_store.loadingShow = false;
		},
	},
}
